import {check, openSettings, request, RESULTS} from 'react-native-permissions';

/**
 * check permission
 * @param value
 * @returns {Promise<boolean|boolean|*|undefined>}
 */
export const checkPermission = async value => {
  try {
    const result = await check(value);
    if (result === RESULTS.GRANTED) {
      return true;
    } else {
      const requestResult = await request(value);
      if (requestResult === RESULTS.GRANTED) {
        return true;
      } else {
        await openSettings();
        return check(value);
      }
    }
  } catch (e) {
    return false;
  }
};
